import store from '@/store';
import {
	// mutations
	setMallHomeData,
	setMallDetailData,
	setMallCommentData,
	//actions
	LOAD_MALL_HOME_DATA,
	LOAD_MALL_DETAIL_DATA,
	INSERT_PRODUCT_SHARE,
	//
	GET_CONFIRM_ORDER_DATA,
	INSERT_ORDER_TO_SERVICE,
	PAY_ORDER,
	//
	LOAD_MALL_COMMENT_DATA,
	GET_COMMENT_DETAIL,
	GET_SHOP_COMMENT_REPLY_LIST,
	DELETE_SHOP_COMMENT,
	INSERT_SHOP_COMMENT_REPLY,
	DELETE_SHOP_COMMENT_REPLY,
	UPDATE_LIKE_COMMENT,
	UPDATE_LIKE_COMMENT_REPLY,
} from './actions';

function updateCommentReplayCount(state, { commentId, productId, isAdd, collectCouontTotal = 0 }) {
	const count = isAdd ? 1 : -1;
	// 更新商品详情的评论数
	const { mallDetailData } = state;
	const existData = mallDetailData[productId] || {};
	state.mallDetailData = {
		...mallDetailData,
		[productId]: {
			...existData,
			// product: {
			// 	...existData.product,
			// 	comtCnt: existData.product ? existData.product.comtCnt + count : 0,
			// },
			comments: existData.comments ? existData.comments.map(item => {
				if (item.id === commentId) {
					return {
						...item,
						replayCount: item.replayCount + count,
						collectCouont: isAdd ? item.collectCouont : item.collectCouont - collectCouontTotal,
					};
				}
				return { ...item };
			}) : [],
		}
	};
	// 更新列表的评论数
	const { mallCommentData } = state;
	state.mallCommentData = mallCommentData.map(item => {
		if (item.id === commentId) {
			return {
				...item,
				replayCount: item.replayCount + count,
				collectCouont: isAdd ? item.collectCouont : item.collectCouont - collectCouontTotal,
			};
		}
		return { ...item };
	});
}

const prefixUrl = () => uni.$http.commodityPrefixUrl;
export default {
	state:{
		name: 'commodity',
		mallHomeData: [],
		mallDetailData: { '': { skuStockList: [], product: {}, comments: [], feightTemplate: {} } },
		//
		confirmOrderData: { memberReceiveAddressList: [], cartPromotionItemList: [] },
		//
		mallCommentData: [],
		commentDetailData: { commentRepls: [] },
	},
	getters: {

	},
	mutations: {
		[setMallHomeData] (state, { data }) {
			state.mallHomeData = data;
		},
		[setMallDetailData] (state, { data, isRefreshSkuList, params = {} }) {
			const { mallDetailData } = state;
			const { id: productId } = params;
			if (isRefreshSkuList) {
				// const existData = mallDetailData[productId] || {};
				// state.mallDetailData = {
				// 	...mallDetailData,
				// 	[productId]: {
				// 		...existData,
				// 		skuStockList: data.skuStockList || [],
				// 	},
				// };
				// 因需同步限购数量改为以下
				state.mallDetailData = {
					...mallDetailData,
					[productId]: data,
				};
			} else {
				state.mallDetailData = {
					...mallDetailData,
					[productId]: data,
				};
			}
		},
		[setMallCommentData] (state, { data, isRefresh }) {
			if (isRefresh) {
				state.mallCommentData = data;
			} else {
				state.mallCommentData = [...state.mallCommentData, ...data];
			}
		},
		//
		[GET_CONFIRM_ORDER_DATA] (state, { data }) {
			state.confirmOrderData = data;
		},
		//
		[GET_COMMENT_DETAIL] (state, { data, isLoadMore }) {
			if (isLoadMore) {
				const { commentDetailData } = state;
				state.commentDetailData = {
					...commentDetailData,
					commentRepls: [...commentDetailData.commentRepls, ...data],
				};
			} else {
				state.commentDetailData = data;
			}
		},
		// 回复评论及回复评论下的回复
		[INSERT_SHOP_COMMENT_REPLY] (state, { data }) {
			const { commentId, productId } = data;
			const { commentDetailData } = state;
			state.commentDetailData = {
				...commentDetailData,
				replayCount: commentDetailData.replayCount + 1,
				commentRepls: [data, ...commentDetailData.commentRepls],
			};
			updateCommentReplayCount(state, { commentId, productId, isAdd: true });
		},
		// 删除评论的回复
		[DELETE_SHOP_COMMENT_REPLY] (state, { params }) {
			const { commentReplId, commentId, productId/*, collectCouontTotal = 0*/ } = params;
			const { commentDetailData } = state;
			state.commentDetailData = {
				...commentDetailData,
				replayCount: commentDetailData.replayCount - 1,
				// collectCouont: commentDetailData.collectCouont - collectCouontTotal,
				commentRepls: commentDetailData.commentRepls.filter(item => item.id !== commentReplId),
			};
			updateCommentReplayCount(state, { commentId, productId, isAdd: false, collectCouontTotal: 0 });
		},
		[DELETE_SHOP_COMMENT] (state, { params }) {
			const { productId, commentId, replayCount = 0 } = params;
			const { mallCommentData } = state;
			// 更新评论列表
			state.mallCommentData = mallCommentData.filter(item => item.id !== commentId);
			// 更新商品详情
			const { mallDetailData } = state;
			const existData = mallDetailData[productId] || {};
			const { product = {}, comments = [] } = existData;
			state.mallDetailData = {
				...mallDetailData,
				[productId]: {
					...existData,
					product: {
						...product,
						comtCnt: product.comtCnt ? product.comtCnt - replayCount : 0,
					},
					comments: comments.filter(item => item.id !== commentId),
				},
			};
		},
		[UPDATE_LIKE_COMMENT] (state, { params }) {
			const { productId, status, commentId } = params;
			const { mallDetailData, mallCommentData } = state;
			// 评论详情
			const { commentDetailData } = state;
			state.commentDetailData = {
				...commentDetailData,
				likeSt: status,
				collectCouont: status ? commentDetailData.collectCouont + 1 : commentDetailData.collectCouont - 1,
			};
			// 更新商品详情的点赞数
			const existData = mallDetailData[productId] || {};
			const { comments = [] } = existData;
			state.mallDetailData = {
				...mallDetailData,
				[productId]: {
					...existData,
					comments: comments.map(item => ({
						...item,
						likeSt: item.id === commentId ? status : item.likeSt,
						collectCouont: item.id === commentId ? (status ? item.collectCouont + 1 : item.collectCouont - 1) : item.collectCouont,
					})),
				},
			};
			//  更新商品评论列表点赞数
			state.mallCommentData = mallCommentData.map(item => ({
				...item,
				likeSt: item.id === commentId ? status : item.likeSt,
				collectCouont: item.id === commentId ? (status ? item.collectCouont + 1 : item.collectCouont - 1) : item.collectCouont,
			}));
		},
		[UPDATE_LIKE_COMMENT_REPLY] (state, { params }) {
			const { status, commentReplId, commentId } = params;
			const { commentDetailData } = state;
			// 更新商品详情
			// const { mallDetailData/*, mallCommentData*/ } = state;
			// const { comments = [] } = mallDetailData;
			// state.mallDetailData = {
			// 	...mallDetailData,
			// 	comments: comments.map(item => {
			// 		if (item.id === commentId) {
			// 			return {
			// 				...item,
			// 				// collectCouont: item.id === commentId ? (status ? item.collectCouont + 1 : item.collectCouont - 1) : item.collectCouont,
			// 			};
			// 		}
			// 		return { ...item };
			// 	}),
			// };
			// 评论详情
			state.commentDetailData = {
				...commentDetailData,
				// collectCouont: status ? commentDetailData.collectCouont + 1 : commentDetailData.collectCouont - 1,
				commentRepls: commentDetailData.commentRepls.map(item => {
					if (item.id === commentReplId) {
						return {
							...item,
							collCnt: item.id === commentReplId ? (status ? item.collCnt + 1 : item.collCnt - 1) : item.collCnt,
						};
					}
					return { ...item };
				}),
			};
			//  更新商品评论列表点赞数
			// state.mallCommentData = mallCommentData.map(item => ({
			// 	...item,
			// 	collectCouont: item.id === commentId ? (status ? item.collectCouont + 1 : item.collectCouont - 1) : item.collectCouont,
			// }));
		},
	},
	actions: {
		async [LOAD_MALL_HOME_DATA] ({ commit }, params) {
			try {
				const res = await uni.$http.get(prefixUrl() + '/home/content', params);
				if (res && res.code === 200 && res.data) {
					const { data = [] } = res;
					commit(setMallHomeData, { data });
				}
				return res;
			} catch (error) {
				console.error('LOAD_MALL_HOME_DATA', error);
				return error;
			}
		},
		async [LOAD_MALL_DETAIL_DATA] ({ commit }, params) {
			const initData = { skuStockList: [], product: {}, comments: [], feightTemplate: {} };
			try {
				const { isRefreshSkuList, ...rest } = params;
				const res = await uni.$http.get(prefixUrl() + '/product/detail', rest);
				if (res && res.code === 200) {
					const data = res.data || initData;
					commit(setMallDetailData, { data, isRefreshSkuList, params });
				}
				return res;
			} catch (error) {
				console.error('LOAD_MALL_DETAIL_DATA', error);
				commit(setMallDetailData, { data: initData });
				return error;
			}
		},
		async [INSERT_PRODUCT_SHARE] ({ commit }, params) {
			try {
				const res = await uni.$http.post(prefixUrl() + '/product/share', params);
				return res;
			} catch (error) {
				console.error('INSERT_PRODUCT_SHARE', error);
				return error;
			}
		},
		//
		async [GET_CONFIRM_ORDER_DATA] ({ commit }, params) {
			try {
				const res = await uni.$http.post(prefixUrl() + '/order/generateConfirmOrderByProd', params);
				if (res && res.code === 200) {
					const { data } = res;
					commit(GET_CONFIRM_ORDER_DATA, { data });
				}
				return res;
			} catch (error) {
				console.error('GET_CONFIRM_ORDER_DATA', error);
				return error;
			}
		},
		async [INSERT_ORDER_TO_SERVICE] ({ commit }, params) {
			try {
				const res = await uni.$http.post(prefixUrl() + '/order/generateOrder', params);
				// console.log('INSERT_ORDER_TO_SERVICE', res);
				return res;
			} catch (error) {
				console.error('INSERT_ORDER_TO_SERVICE', error);
				return error;
			}
		},
		async [PAY_ORDER] ({ commit }, params) {
			try {
				const res = await uni.$http.post(prefixUrl() + '/order/paySuccess', params);
				return res;
			} catch (error) {
				console.error('PAY_ORDER', error);
				return error;
			}
		},
		//
		async [LOAD_MALL_COMMENT_DATA] ({ commit }, params) {
			try {
				const { isRefresh, ...rest } = params;
				const res = await uni.$http.get(prefixUrl() + '/comment/getCommentList', rest);
				if (res && res.code === 200) {
					const { data = [] } = res;
					commit(setMallCommentData, { data, isRefresh });
				}
				return res;
			} catch (error) {
				console.error('LOAD_MALL_COMMENT_DATA', error);
				return error;
			}
		},
		async [GET_COMMENT_DETAIL] ({ commit }, params) {
			try {
				const res = await uni.$http.get(prefixUrl() + '/comment/getCommentDetail', params);
				if (res && res.code === 200) {
					const { data } = res;
					commit(GET_COMMENT_DETAIL, { data });
				}
				return res;
			} catch (error) {
				console.error('GET_COMMENT_DETAIL', error);
				return error;
			}
		},
		async [GET_SHOP_COMMENT_REPLY_LIST] ({ commit }, params) {
			try {
				const res = await uni.$http.get(prefixUrl() + '/comment/getCommentReplList', params);
				if (res && res.code === 200) {
					const { data } = res;
					commit(GET_COMMENT_DETAIL, { data, isLoadMore: true });
				}
				return res;
			} catch (error) {
				console.error('GET_SHOP_COMMENT_REPLY_LIST', error);
				return error;
			}
		},
		async [INSERT_SHOP_COMMENT_REPLY] ({ commit }, params) {
			try {
				const res = await uni.$http.post(prefixUrl() + '/comment/addCommentRepl', params);
				if (res && res.code === 200) {
					const { icon, nickname, id } = store.state.app.umsMember;
					const {
						productId, // 商品id
						commentId, // 评论id
						upReplId, // 回复id
						content, // 评论回复内容
						modDt,
						replMemberId, // 回复回复的时候上级回复用户id
						replMemberNickName,
					} = params;
					// collectCouont: 0
					// commentId: 37
					// content: "阿斯顿发送到"
					// id: 5
					// likeSt: 0
					// memberIcon: "https://dpc.way-s.cn/mvp/staticFile/20210511/6d74cb1a53074916bf0eda63e2260c2a.png"
					// memberNickName: "什么是快乐星球"
					// modDt: "2021/05/25"
					// modId: 20007
					// regiDt: "2021/05/25"
					// regiId: 20007
					// replMemberNickName: "什么是快乐星球"
					// upReplId: 0
					let data = {
						productId,
						collectCouont: 0,
						commentId,
						content,
						id: res.data,
						likeSt: 0,
						memberIcon: icon,
						memberNickName: nickname,
						modDt,
						modId: id,
						regiDt: modDt,
						regiId: id,
						upReplId,
					};
					if (upReplId) { // 存在说明是评论，回复评论下面的回复评论
						data = {
							...data,
							replMemberId,
							replMemberNickName,
						};
					}
					commit(INSERT_SHOP_COMMENT_REPLY, { data });
					uni.showToast({
						title: res && res.code === 200 ? '发布成功' : '发布失败，请重试',
						duration: 2000,
						icon: 'none'
					});
				}
				return res;
			} catch (error) {
				console.error('INSERT_SHOP_COMMENT_REPLY', error);
				return error;
			}
		},
		async [DELETE_SHOP_COMMENT_REPLY] ({ commit }, params) {
			try {
				commit(DELETE_SHOP_COMMENT_REPLY, { params });
				const res = await uni.$http.get(prefixUrl() + '/comment/delCommentRepl', params);
				if (res && res.code === 200) {
				}
				return res;
			} catch (error) {
				console.error('DELETE_SHOP_COMMENT_REPLY', error);
				return error;
			}
		},
		async [DELETE_SHOP_COMMENT] ({ commit }, params) {
			try {
				commit(DELETE_SHOP_COMMENT, { params });
				const res = await uni.$http.get(prefixUrl() + '/comment/delComment', params);
				if (res && res.code === 200) {
				}
				return res;
			} catch (error) {
				console.error('DELETE_SHOP_COMMENT_REPLY', error);
				return error;
			}
		},
		async [UPDATE_LIKE_COMMENT] ({ commit }, params) {
			try {
				commit(UPDATE_LIKE_COMMENT, { params });
				const res = await uni.$http.post(prefixUrl() + '/comment/commentLike', params);
				if (res && res.code === 200) {
				}
				return res;
			} catch (error) {
				console.error('UPDATE_LIKE_COMMENT', error);
				return error;
			}
		},
		async [UPDATE_LIKE_COMMENT_REPLY] ({ commit }, params) {
			try {
				commit(UPDATE_LIKE_COMMENT_REPLY, { params });
				const res = await uni.$http.post(prefixUrl() + '/comment/commentReplLike', params);
				if (res && res.code === 200) {
				}
				return res;
			} catch (error) {
				console.error('UPDATE_LIKE_COMMENT_REPLY', error);
				return error;
			}
		},
	}
}
